package juc.threadlocal;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName UseThreadLocal
 * @PackageName threadlocal
 * @projectName myThread
 * @Description 
 * @Author games
 * @Date 2025/8/27 下午1:21
 * @Version 1.0
 */
public class UseThreadLocal {

    public class ThreadLocalExample {
        private static ThreadLocal<String> nameThreadLocal = new ThreadLocal<>();
        private static ThreadLocal<Integer> ageThreadLocal = new ThreadLocal<>();
        private static ThreadLocal<List<String>> hobbyThreadLocal = new ThreadLocal<>();

        public static void main(String[] args) {
            // 当前线程Thread对象内部的ThreadLocalMap会存储以下三个Entry
            nameThreadLocal.set("张三");
            ageThreadLocal.set(25);
            hobbyThreadLocal.set(Arrays.asList("读书", "游泳"));

            // 获取值 - 都从当前线程的ThreadLocalMap中查找
            System.out.println(nameThreadLocal.get());    // "张三"
            System.out.println(ageThreadLocal.get());     // 25
            System.out.println(hobbyThreadLocal.get());   // ["读书","游泳"]
        }
    }


}
